package dyyx.discard;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.time.LocalTime;

import org.apache.commons.lang3.StringUtils;


public class DiscardClient {


	static final String HOST = System.getProperty("host", "127.0.0.1");
    static final int PORT = Integer.parseInt(System.getProperty("port", "8009"));

    private static final String UTF8 = "UTF-8";

    
    private static long seq = 0;

	public static void main(String[] args) throws Exception {
		
		System.out.println("host="+HOST+",port="+PORT);
		
		Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandle());


		Socket socket = new Socket();
		
		SocketAddress socketAddress = new InetSocketAddress(HOST ,PORT); 
		socket.connect(socketAddress,5000); 
        
        
		socket.setKeepAlive(true);
		socket.setTcpNoDelay(true);
		// socket.setSoLinger(on, linger);
		//socket.setSoTimeout(timeout);
		// socket.getInputStream();
		
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),UTF8));
		
		while(true){
			writer.write(getMsg());
			writer.write("\n");
			writer.flush();
			System.out.println(seq+","+LocalTime.now());
			Thread.sleep(300);
		}
				
	}
	
    static String getMsg(){
    	return (seq++) + StringUtils.repeat("-X", 1024*100);
    }	
	
	
	
	
	static class MyUncaughtExceptionHandle implements Thread.UncaughtExceptionHandler {
	    @Override
	    public void uncaughtException(Thread t, Throwable e) {
	        System.out.println(t +" run error,"+e);
	    }
	}

	
}
